#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    exgcd(b, a % b, x, y);
    int t;
    t = x;
    x = y;
    y = t - (a / b) * y;
}
int main()
{
    int a, b, x, y;
    cin >> a >> b;
    exgcd(a, b, x, y);
    if(x < 0)
    {
        x = (x + b) % b;
    }
    cout << x << endl;
    return 0;
}